<?php
namespace app\home\controller;

use think\Controller;
use think\Cookie;

// 公共控制器
use app\common\controller\Home AS HomeController;

// 收藏控制器
class Collect extends HomeController{
    // 构造函数
    public function __construct()
    {
        // 继承底层的控制器构造函数(底层父类构造函数)
        parent::__construct();

        // 定义一个全局模型
        $this->UserModel=model('User.User');
        $this->PostModel=model('Post.Post');
        $this->CollectModel=model('User.Collect');
    }

    // 添加收藏
    public function add(){
        $success=[
            'result'=>false,
            'msg'=>''
        ];

        // 判断是否有post数据
        if($this->request->isPost()){
            $postid=$this->request->param('postid',0);

            // 根据id去查询这个帖子是否存在
            $post=$this->PostModel->find($postid);

            if(!$post){
                $this->error('收藏的帖子不存在');
                exit;
            }

            // 判断当前这个人是否有收藏过
            $where=[
                'userid'=>$this->LoginUser['id'],
                'postid'=>$postid
            ];

            $collect=$this->CollectModel->where($where)->find();

            if($collect){
                $success['result']=false;
                $success['msg']='您已收藏此贴，无须重复收藏';
                return json($success);
            }

            // 组装数据
            $data=[
                'userid'=>$this->LoginUser['id'],
                'postid'=>$postid
            ];

            // 添加收藏
            $result=$this->CollectModel->validate('common/User/Collect')->save($data);

            if($result===FALSE){
                // 添加收藏失败
                $success['result']=false;
                $success['msg']=$this->CollectModel->getError();
            }else{
                // 添加收藏成功
                $success['result']=true;
                $success['msg']='收藏成功';
            }

            // 将结果返回给Ajax
            return json($success);
        }
    }

    // 取消收藏
    public function del(){
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isPost()){
            $postid = $this->request->param('postid', 0);

            //根据id去查询这个帖子是否存在
            $post = $this->PostModel->find($postid);

            if(!$post){
                $this->error('收藏的帖子不存在');
                exit;
            }

            //判断当前这个人是否有收藏过
            $where = [
                'userid' => $this->LoginUser['id'],
                'postid' => $postid
            ];

            $collect = $this->CollectModel->where($where)->find();

            $data = [
                'userid' => $this->LoginUser['id'],
                'postid' => $postid
            ];
            
            if($collect){
                $success['result'] = false;
                $result = $this->CollectModel->destroy($data);


                if($result === FALSE){
                    // 添加收藏失败
                    $success['result'] = false;
                    $success['msg'] = $this->CollectModel->getError();
                }else{
                    //添加收藏成功
                    $success['result'] = true;
                    $success['msg'] = '取消成功';
                }
    
                //将结果返回给ajax
                return json($success);
            }

            $success['msg'] = '取消收藏此贴';
            return json($success);
        }

        
    }
}

?>